package com.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oracle.mapper.ClazzMapper;
import com.oracle.pojo.Clazz;
import com.oracle.service.IClazzService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("clazzServiceImpl")   // 指定当前实现类的id
@Transactional
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements IClazzService {
    @Override
    public IPage<Clazz> getClazzsByConditionForPage(Page<Clazz> clazzPage, String gradeName, String name) {
        QueryWrapper<Clazz> clazzQueryWrapper = new QueryWrapper<>();
        // 如果年级名称不为空，则在模糊查询的条件上拼接年级名称
        if (!StringUtils.isEmpty(gradeName)) {
            clazzQueryWrapper.like("grade_name",gradeName);
        }
        // 如果班级名称不为空，则在模糊查询的条件上拼接班级名称
        if (!StringUtils.isEmpty(name)) {
            clazzQueryWrapper.like("name",name);
        }
        // 设置排序规则，按照id排序(降序)
        clazzQueryWrapper.orderByDesc("id");
        Page<Clazz> page = baseMapper.selectPage(clazzPage, clazzQueryWrapper);

        return page;

    }

    @Override
    public List<Clazz> getClazzs() {
        List<Clazz> clazzList = baseMapper.selectList(null);
        return clazzList;
    }
}
